import random
import common
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
#或者使用下面的设置, 提升速度
options.add_argument('blink-settings=imagesEnabled=false')
# 此步骤很重要，设置为开发者模式，防止被各大网站识别出来使用了Selenium
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 后面是你的浏览器驱动位置，记得前面加r'','r'是防止字符转义的
driver = webdriver.Chrome(r'C:\Users\Administrator\AppData\Local\Programs\Python\Python38\chromedriver.exe',options=options)
driver.maximize_window()
script='''Object.defineProperties(navigator, {webdriver:{get:()=>undefined}})'''
driver.execute_script(script)
sleep(500)
# 用get打开百度页面
driver.get("https://www.baidu.com")
sleep(2)
# 查找页面的“设置”选项，并进行点击
# driver.find_elements_by_link_text('设置')[0].click()
# sleep(2)
# # 打开设置后找到“搜索设置”选项，设置为每页显示50条
# driver.find_elements_by_link_text('搜索设置')[0].click()
# sleep(2)
try:
    bdinput = driver.find_element_by_id('kw').send_keys('青岛网站')
    sleep(2)
    bdinput = driver.find_element_by_id('kw').send_keys('制作')
    sleep(1)
    bdinput = driver.find_element_by_id('kw').send_keys('公司')
    sleep(20)

    # driver.find_element_by_id('su').click()
    # '''通过键盘回车来代替搜素按钮的点击操作'''
    driver.find_element_by_id("su").send_keys(Keys.ENTER)
    sleep(5)
    # driver.find_elements_by_link_text('')[0].click()
    # line_list = driver.find_elements_by_xpath("//h3[contains(@class,'t')]") #优化站和普通竞价 医疗竞价定为不到
    line_list = driver.find_elements_by_class_name("c-showurl")
    # print(123);
    # 竞价开始
    # SEMS = driver.find_elements_by_xpath('//*[@id]/div[3]/a/span') #竞价
    # for sem in SEMS:
    #     print ('%s - %s' % (sem.text, type(sem.text)))
    #     if u'青岛静康中医肾脏病' in sem.text:
    #        print ('yes')
    #        sem.click()
    #        sleep(10)
    # 竞价结束
    # '''xpath提取特征'''
    for line in line_list:
        # t = line.find_element_by_xpath("a")
        # print ('%s - %s' % (line.text, type(line.text)))
        if u'yujianlong' in line.text:
           line.click()
           sleep(random.randint(3, 10))
           wins = driver.window_handles
           driver.switch_to.window(wins[-1])

           catitems = driver.find_elements_by_xpath("//ul/li[contains(@class,'cat-item-20')]/a")
           # print(catitems)
           catitems_len = len(catitems)
           random_catitem = random.randint(20, catitems_len)
           catitems[random_catitem].click()
           sleep(random.randint(3, 10));
           # driver.refresh()
           # driver.switch_to.window(wins[-1])
           catitems1 = driver.find_elements_by_xpath("//ul[contains(@class,'widget-hot')]/li/a")
           # for c in catitems1:
           #     print(c.text)
           catitems_len1 = len(catitems1)
           random_catitem1 = random.randint(0, catitems_len1)
           catitems1[random_catitem1].click()
           sleep(random.randint(5, 10));
           break
           # print(driver.current_window_handle)

    # print ('test pass: element found by link text')
    
except Exception as e:
    print ("YuTest", format(e))


# 关闭浏览器
random.randint(3, 15)
driver.quit()